package com.atguigu.admin.config;

import com.atguigu.admin.interceptor.LoginInterceptor;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * @author 杨林
 * @create 2023-08-30 8:59 星期三
 * description:
 */

/**
 * 1、编写一个拦截器实现HandlerInterceptor接口
 * 2、拦截器注册到容器中 （实现WebMvcConfigurer接口的addInterceptors()方法）
 * 3、指定拦截规则（如果拦截所有，静态资源也会被拦截）
 * 
 * @EnableWebMvc:全面接管SpringMVC
 *  全面接管之后SpringMVC后官方的自动配置全部失效
 */
//@EnableWebMvc    //慎用
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

    /**
     * 定义静态资源行为
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 访问/static/**的所有请求都去classpath:/static/路径下进行匹配
         */
        registry.addResourceHandler("/static/**")
        .addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")       //拦截所有请求   静态资源也会被拦截
                .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**","/sql,/city/**");   //放行这两个请求
    }
    
    
    
//    @Bean
//    public WebMvcRegistrations webMvcRegistrations(){
//        return new WebMvcRegistrations(){
//            @Override
//            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
//                return null;
//            }
//        };
//    }
}
